home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9310 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
  4. Subject: Re: Access carry flag from C
  5. Date: 28 Feb 1996 08:21:44 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h1veoINNlns@anvil.ugrad.cs.ubc.ca>
  8. References: <Dn1C9z.DGv.0.net@indra.com> <824853272snz@genesis.demon.co.uk> <4gqj0d$d6p@airdmhor.gen.nz> <825377932snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <825377932snz@genesis.demon.co.uk>,
  12. Lawrence Kirby  <fred@genesis.demon.co.uk> wrote:
  13.  >In article <4gqj0d$d6p@airdmhor.gen.nz>
  14.  >           gumboot@airdmhor.gen.nz "Simon Hosie" writes:
  15.  >
  16.  >>Lawrence Kirby:
  17.  >>> It certainly can be done portably although not with the efficiency that
  18.  >>> a platform-specific solution is likely to give you.
  19.  >>
  20.  >>>     int j, k;
  21.  >>
  22.  >>>     ...
  23.  >>
  24.  >>>     if ((j >= 0) ? (k > INT_MAX-j) : (k < INT_MIN-j))
  25.  >>                     ^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^
  26.  >>  Won't they both be evaluated?
  27.  >
  28.  >No, given:
  29.  >
  30.  >   (expr1) ? expr2 : (expr3)
  31.  >
  32.  >the (ANSI) C language guarantees that expr1 will be evaluated and followed
  33.  >by a sequence point. If expr1 evaluates to a value that compares unequal
  34.  >to zero then expr2 is evaluated, otherwise expr3 is evaluated. Under no
  35.  >circumstances (except undefined behaviour caused by something else in the
  36.  >program, where anything can happen) can both expr2 and expr3 be evaluated.
  37.  >
  38.  >You probably noticed that I didn't put parentheses around expr2. Can you
  39.  >see why they are necessary around expr1 and expr3 but not here? (This does
  40.  >test understanding of expression parsing quite deeply).
  41.  
  42. Not really. The ?: has a very low precedence, but there are expressions that
  43. have an even lower precedence; namely assignment expressions and comma
  44. expressions.
  45.  
  46. If (expr1) was written without the brackets, and contained a comma or
  47. assignment expression, then the rest of the statement would get interpreted as
  48. just the rightmost branch  of the comma list, or just the right side of the
  49. assignment expression.
  50.  
  51. Same goes for expr3, execpt s/right/left/
  52.  
  53. The middle expression does not need this because the parser is already in the
  54. middle of the production. It has seen the ?, so it knows it has the prefix of
  55. a conditional expression. In effect, the ? and : symbols already act
  56. analogously to parentheses.
  57. -- 
  58.  
  59.